  var u = navigator.userAgent,
        isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端

    function connectWebViewJavascriptBridge(callback) {
        if (window.WebViewJavascriptBridge) {
            callback(WebViewJavascriptBridge)
        } else {
            if (!isAndroid) {
                if (window.WVJBCallbacks) {
                    return window.WVJBCallbacks.push(callback);
                }
                window.WVJBCallbacks = [callback];
                var WVJBIframe = document.createElement('iframe');
                WVJBIframe.style.display = 'none';
                WVJBIframe.src = 'https://__bridge_loaded__';
                document.documentElement.appendChild(WVJBIframe);
                setTimeout(function () {
                    document.documentElement.removeChild(WVJBIframe)
                }, 0)
            } else {
                document.addEventListener('WebViewJavascriptBridgeReady', function (event) {
                    callback(WebViewJavascriptBridge)
                }, false)
            }
        }
    }

      connectWebViewJavascriptBridge(function (bridge) {
        bridge.init(function (message, responseCallback) {
            responseCallback({ 'Javascript Responds': 'Wee!' })
        })
        bridge.registerHandler('callJsBack', function (data, responseCallback) {
        	//是否退出App
           var index=$('#index').val();
           if(index==1){
           	 $('.admonitory').css('display','block');
           }else if(index==undefined){
          	  var recurrence=$('#recurrence').val();
          	  var recurrence=Number(recurrence);
          	  if(recurrence==1){
          	  	location.href = "../WxBooks/make.html";
          	  }else if(recurrence==2){
          	  		location.href = "../photoNewbook/my.html";
          	  }else if(recurrence==0){
          	  		 window.history.go(-1);
          	  }
           }
           //调返回事件
           
        });
        //把图片放到图片库 

				 bridge.registerHandler('storehouse',function(repsonseData){
				 		var	repsonseData=JSON.parse(repsonseData);
				 		var time=repsonseData.time;
						var url=repsonseData.filepath;
						var pbId = sessionStorage.getItem("pbId");
						var userId = sessionStorage.getItem("userId");
					            $.ajax({
													    type:"POST",
													    url:"http://www.youyiku-yishu.cn/WebApp/AppPhotoBooks/uploadToImgDepot",
													    data:{url:url,pbId:pbId,userId:userId,time:time},
													    dataType:"json",
													       beforeSend: function (XMLHttpRequest) {
													         		XMLHttpRequest.setRequestHeader("DEVICEINFO", "JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU");
													       }
													  }).then(data=>{
													  	var quantity=$('.quantity_span').text();
															var quantity=Number(quantity);
															var quantity=quantity+1;
															$('.quantity_span').text(quantity);
															$('.quantity').css('display','block');
													  	 warn5();
													     warn3();
									 })
				});  
				//语音转文字
					 bridge.registerHandler('recognition',function(repsonseData){
				 		var	Text=repsonseData;
				 		var num=$('.num').val();
				 		$('.text'+num).html(Text);
						var pbId = sessionStorage.getItem("pbId");
						var page=$('#current_page').val();
						var direction=$('.ranking').val();
						 var none_no=$('#none_no').val();
						  none_no=Number(none_no);
						  if(none_no==0){
              	warn8();
              }else{
					            $.ajax({
													    type:"POST",
													    url:"http://www.youyiku-yishu.cn/WebApp/AppPhotoBooks/saveTxt",
													    data:{Text:Text,pbId:pbId,page:page,direction:direction},
													    dataType:"json",
													       beforeSend: function (XMLHttpRequest) {
													         		XMLHttpRequest.setRequestHeader("DEVICEINFO", "JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU");
													       }
													  }).then(data=>{
													  	if(data.status==1){
													  		 warn7();
													  	 $('.shade').css('display','none');
                               $('.voice_1').css('display','none');
                               $('.none_no').css('display','none');
													  	}else{
													  		 $('.shade').css('display','none');
                                 $('.voice_1').css('display','none');
                                 $('.none_no').css('display','none');
                               		warn7();
                               		warn1();
													  	}
													  	
                              
									 })
								 }
				});
				//上传语音
			  bridge.registerHandler('pronunciation',function(repsonseData){
					 		var	repsonseData=JSON.parse(repsonseData);
					 		var url=repsonseData.path;
              var page=$('#current_page').val();
              var direction=$('.ranking').val();
              var num=$('.num').val();
              var pbId = sessionStorage.getItem("pbId");
              var none_no=$('#none_no').val();
              none_no=Number(none_no);
              if(none_no==0){
              	warn8();
              }else{            	            
               $.ajax({
												 type:"POST",
												 url:"http://www.youyiku-yishu.cn/WebApp/AppPhotoBooks/saveVideo",
												 data:{url:url,pbId:pbId,page:page,direction:direction},
												 dataType:"json",
												 beforeSend: function (XMLHttpRequest) {
									    			  XMLHttpRequest.setRequestHeader("DEVICEINFO", "JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU");
									       } 
							       }).then(json=>{                                                                                        
                                 if(json.status==1){
                                 var page="";
                                 page+='<div class="erWeiMa" data-url="'+url+'"></div>'
                                 $('.text'+num).html(page);
                                  tobecode();
                                   $('.shade').css('display','none');
                                   $('.voice_2').css('display','none');
                                   $('.none_no').css('display','none');                             
                                    warn7();
                                  }else{
                                  	$('.none_no').css('display','none');                                   
                                    $('.shade').css('display','none');
                                    $('.voice_2').css('display','none');
                                    $('.none_no').css('display','none');
                                    warn7();
                                    warn1();
                                   
                                  }
                                })                                                 
                    }
											});
				//视频上传
				bridge.registerHandler('videoupload',function(repsonseData){
							var	repsonseData=JSON.parse(repsonseData);
							var url=repsonseData.path;
              var page=$('#current_page').val();
              var direction=$('#direction').val();
              var pbId = sessionStorage.getItem("pbId"); 
             	var isvedio=1;
               $.ajax({
												 type:"POST",
												 url:"http://www.youyiku-yishu.cn/WebApp/AppPhotoBooks/uploadPhoto",
												 data:{url:url,pbId:pbId,page:page,direction:direction,isvedio:isvedio},
												 dataType:"json",
												 beforeSend: function (XMLHttpRequest) {
									    			  XMLHttpRequest.setRequestHeader("DEVICEINFO", "JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU");
									       } 
							       }).then(json=>{
                                 if(json.status==1){
                                 	  
                              		  var num=$('#imgbox').val();
                                    var page="";
                                    page+='<div class="vedioToCode" data-url="'+url+'">';
                                    page+='</div>';
//                                  page+='<div class="upload-div"></div>';
                                    $('.img'+num).html(page);
                                    toBecomeCode();
                                    $('.shade4').css('display','none');
                                    $('.loading').css('display','none');
                                    $('.previous').css('display','block');
                                    $('.next').css('display','block');
                                    $('.FunctionBar').css('display','block');
                                    $('.upload-div').css('display','none');
                                     $('.shade').css('display','none');
                                    $('.Uploadmode').css('display','none');
                                    $('#remove').val(0);
                                    warn5();
                                  }else{
                                          warn1();
							                            $('.shade4').css('display','none');
							                            $('.loading').css('display','none');
							                            $('.upload-div').css('display','none');
							                            $('.shade').css('display','none');
                                          $('.Uploadmode').css('display','none');
							                            $('#remove').val(0);
                                  }



                                })                                                
                   
											});			
		  //单图上传
		  bridge.registerHandler('leaf',function(repsonseData){
							var	repsonseData=JSON.parse(repsonseData);
							var url=repsonseData.filepath;
              var page=$('#current_page').val();
              var direction=$('#direction').val();
              var pbId = sessionStorage.getItem("pbId"); 
             	var isvedio=$('#isvedio').val();
               $.ajax({
												 type:"POST",
												 url:"http://www.youyiku-yishu.cn/WebApp/AppPhotoBooks/uploadPhoto",
												 data:{url:url,pbId:pbId,page:page,direction:direction},
												 dataType:"json",
												 beforeSend: function (XMLHttpRequest) {
									    			  XMLHttpRequest.setRequestHeader("DEVICEINFO", "JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU");
									       } 
							       }).then(json=>{
							       	
                                 if(json.status==1){
                                  	 warn5();
                              		  var nue = $('#imgbox').val();
                                    var page = "";
                                    var ROOT = "http://www.youyiku-yishu.cn";
                                     var appearance=json.appearance;
                                    appearance=Number(appearance);
		                                if(appearance==0 || appearance==2){
		                                	  page+='<img src="' + ROOT + '/'+url+'" onclick="class="appearance'+appearance+' targetObj_1" id=""style="width:100%">';
		                                }else{
		                                	page+='<img src="' + ROOT + '/'+url+'" onclick="class="appearance'+appearance+' targetObj_1" id=""style="height:100%">';
		                                }
                                    $('.img' + nue).html('');
                                    $('.img' + nue).html(page);
                                    $('.shade').css('display', 'none');
                                    var file1 = $('.file' + direction);
                                    file1.outerHTML = '';
                                    $('.upload-btn1').html('');
                                    $('.Uploadmode').css('display', 'none');
                                    $('.previous').css('display','block');
                                    $('.next').css('display','block');
                                    $('.FunctionBar').css('display','block');
                                  }else{
                                    warn1();
                                    $('.Uploadmode').css('display', 'none');
                                    $('.FunctionBar').css('display','block');
                                  }



                                })                         
											});	
											//封面上传
											  bridge.registerHandler('cover',function(repsonseData){
											  	   	var	repsonseData=JSON.parse(repsonseData);
							                var url=repsonseData.filepath;
											  	    var nun=$('#nun').val();
											  	    var cId=$('#cId').val();
											  	    var pbId = sessionStorage.getItem("pbId");
											  	    var userId = sessionStorage.getItem("userId");
															  $.ajax({
																				 type:"POST",
																				 url:"http://www.youyiku-yishu.cn/WebApp/AppPhotoBooks/saveCoverImage",
																				 data:{url:url,pbId:pbId,cId:cId,userId:userId},
																				 dataType:"json",
																				 beforeSend: function (XMLHttpRequest) {
																	    			  XMLHttpRequest.setRequestHeader("DEVICEINFO", "JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU");
																	       } 
											          }).then(data=>{
											       					var	paper=''
											       					if(data.status==1){   
		                              		  paper=''		                                   
		                                    paper += '<img  src="http://www.youyiku-yishu.cn/'+url+'">';
		                                    $('.imgbox'+nun).html(paper);
		                                    $('.imgbox'+nun).css('background-color','#FFFFFF')
		                                  }else{
		                                          warn1();
		                                   
		                                  }											       																       															       																       					
											          })
											  });
											  //取消上传中
											    bridge.registerHandler('loading',function(repsonseData){
											  	    	 document.getElementById('warn_1').style.display="none";
											  	    	
											  });
   });

    //在改function 中添加原生调起js方法
    setupWebViewJavascriptBridge(function (bridge) {
        console.log("执行1");
        bridge.registerHandler("onJsBack", function (data, responseCallback) {
            console.log("执行2");
        });
    })

    // 调用原生相册
    function openAlbum() {
        window.WebViewJavascriptBridge.callHandler(
        	'openNativeAlbum', 
        	{ "maxNum": 10 },
        	function (responseData) {
                var ele = document.getElementById("text_info");
                ele.value = ele.value + responseData;
               
            }
        )
    }
//视频上传
    function video() {
        window.WebViewJavascriptBridge.callHandler(
        	'openNativeVideo', 
        	{ "maxNum": 1 },
        	function (responseData) {
                var ele = document.getElementById("text_info");
                ele.value = ele.value + responseData;
               
            }
        )
    }
 //单图上传
   function oneupload() {
        window.WebViewJavascriptBridge.callHandler(
        	'openNativeAlbum', 
        	{ "maxNum": 1 },
        	function (responseData) {
                var ele = document.getElementById("text_info");
                ele.value = ele.value + responseData;
               
            }
        )
    }
//封面上传
function uploadimg1(nun){
	 var nun=Number(nun)+1;
	 $('#nun').val(nun);
	 window.WebViewJavascriptBridge.callHandler(
        	'openNativeAlbum', 
        	{ "maxNum": 1,"cover":1},
        	function (responseData) {
                var ele = document.getElementById("text_info");
                ele.value = ele.value + responseData;
               
            }
        )
}
    //调用原生支付
    function openPayWeChat() {   	
    	var orderId = sessionStorage.getItem("orderId");
        // type: 1微信，2支付宝
        window.WebViewJavascriptBridge.callHandler(
            'openNativePay',
            {
                'type': '1',
                'orderId': orderId
            },
            function (responseData) {
             	var	responseData=JSON.parse(responseData);
             	code=responseData.code;
             	if(code==1){
             		location.href = "../index.html";
             	}
            }
        )
    }

    //调用原生支付
    function openPayAli() {   
    	var orderId = sessionStorage.getItem("orderId");
        // type: 1微信，2支付宝
        window.WebViewJavascriptBridge.callHandler(
            'openNativePay',
            {
                'type': '2',
                'orderId': orderId
            },
            function (responseData) {
              	var	responseData=JSON.parse(responseData);
             	code=responseData.code;
             	if(code==1){
             		location.href = "../photoNewBook/myorder.html";
             	}
            }
        )
    }

    //调用原生压缩iOS,Android不支持
    function compressVideo() {
        window.WebViewJavascriptBridge.callHandler(
            'compressVideo',
            { 'info': 'openPay' },
            function (repsonseData) {
                alert("收到");
            }
        )
    }

    //关闭当前界面Android,iOS不支持
    function closeNativePage() {
        window.WebViewJavascriptBridge.callHandler(
            "closeNativePage",
            "",
            function (repsonseData) {
            
            }
        )
    }
//调用微信登录
    function wechatlogin() {
        window.WebViewJavascriptBridge.callHandler(
            "weChatLogin",
            "",
            function (repsonseData) {
            		var	repsonseData=JSON.parse(repsonseData)
								var userId=repsonseData.userId;
								sessionStorage.setItem("userId", userId);
								location.href = "index.html";
            }
        )
    }
//开始识别
    function startWeChatVoice() {
        window.WebViewJavascriptBridge.callHandler(
            "weChatVoice",
            { 'type': 'start' },
            function (repsonseData) {
               
            }
        )
    }
//结束语音识别
    function stopWeChatVoice() {
        window.WebViewJavascriptBridge.callHandler(
            "weChatVoice",
            { 'type': 'stop' },
            function (repsonseData) {
                
            }
        )
    }
//开始录音
    function startNativeRecord() {
        window.WebViewJavascriptBridge.callHandler(
            "nativeRecord",
            { 'type': 'start' },
            function (repsonseData) {
               
            }
        )
    }
//结束录音开始上传
    function stopNativeRecord() {
        window.WebViewJavascriptBridge.callHandler(
            "nativeRecord",
            { 'type': 'stop' },
            function (repsonseData) {
             
            }
        )
    }

